<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "person".
 *
 * @property int $id ID
 * @property string $name 姓名
 * @property string $sex 性别
 * @property string $birthday 出生日期
 * @property string $nationality 国籍
 * @property string $id_type 身份证件类型
 * @property string $id_number 身份证号
 * @property string $ethnicity 民族
 * @property string|null $birthplace 出生地
 * @property string $native_place 籍贯
 * @property string|null $address 住址
 * @property string|null $mobile 手机
 * @property string|null $email 邮箱
 * @property string|null $qq QQ
 * @property string|null $weixin 微信
 * @property string|null $marriage 婚姻状况
 * @property string|null $qualification 学历
 * @property string|null $school 毕业学校
 * @property string|null $major 所学专业
 * @property string|null $graduation_date 毕业时间
 * @property string|null $degree 学位
 * @property string $create_time 创建时间
 * @property string|null $update_time 更新时间
 *
 * @property Student[] $students
 * @property StudentStatus[] $studentStatuses
 * @property Teacher[] $teachers
 * @property User[] $users
 */
class Person extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'person';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'sex', 'birthday', 'nationality', 'id_type', 'id_number', 'ethnicity', 'native_place', 'create_time'], 'required'],
            [['sex'], 'string'],
            [['birthday', 'graduation_date', 'create_time', 'update_time'], 'safe'],
            [['name', 'nationality', 'email', 'school', 'major'], 'string', 'max' => 50],
            [['id_type', 'id_number', 'mobile', 'qq', 'weixin', 'degree'], 'string', 'max' => 20],
            [['ethnicity', 'birthplace', 'marriage', 'qualification'], 'string', 'max' => 10],
            [['native_place'], 'string', 'max' => 100],
            [['address'], 'string', 'max' => 200],
            [['id_type', 'id_number'], 'unique', 'targetAttribute' => ['id_type', 'id_number']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '姓名',
            'sex' => '性别',
            'birthday' => '出生日期',
            'nationality' => '国籍',
            'id_type' => '身份证件类型',
            'id_number' => '身份证号',
            'ethnicity' => '民族',
            'birthplace' => '出生地',
            'native_place' => '籍贯',
            'address' => '住址',
            'mobile' => '手机',
            'email' => '邮箱',
            'qq' => 'QQ',
            'weixin' => '微信',
            'marriage' => '婚姻状况',
            'qualification' => '学历',
            'school' => '毕业学校',
            'major' => '所学专业',
            'graduation_date' => '毕业时间',
            'degree' => '学位',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    /**
     * Gets query for [[Students]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getStudents()
    {
        return $this->hasMany(Student::className(), ['GRID' => 'id'])->inverseOf('gR');
    }

    /**
     * Gets query for [[StudentStatuses]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getStudentStatuses()
    {
        return $this->hasMany(StudentStatus::className(), ['GRID' => 'id'])->inverseOf('gR');
    }

    /**
     * Gets query for [[Teachers]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getTeachers()
    {
        return $this->hasMany(Teacher::className(), ['GRID' => 'id'])->inverseOf('gR');
    }

    /**
     * Gets query for [[Users]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUsers()
    {
        return $this->hasMany(User::className(), ['person_id' => 'id'])->inverseOf('person');
    }

    //提供关联数组 ID => GRID, 可用于下拉框
    public function getPersonID()
    {
        // $query=self::find()->select(['ID'])->orderBy('ID')->indexBy('ID');
        // return $query;
        $sql = 'SELECT DISTINCT `id` FROM `person` WHERE NOT EXISTS (SELECT `GRID` FROM `teacher` WHERE teacher.GRID = person.id)';
        $result = Yii::$app->db->createCommand($sql)->queryAll();
        $newResult = [];
        foreach($result as $val) {
          $newResult[] = $val['id'];
        }
        return $newResult;
    }
}
